package com.jjlin.web.admin;

import com.jjlin.entity.Type;
import com.jjlin.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    //展示分类
    @GetMapping("/types")
    public String types(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC)
                                    Pageable pageable, Model model){

        Page<Type> types = typeService.ListType(pageable);
        model.addAttribute("page",types);
        return "admin/types";
    }

    //跳转到增加分类页面
    @GetMapping("/types/input")
    public String inputPage(Model model){
        model.addAttribute("type", new Type());
        return "admin/types-input";
    }

    //跳转到编辑分类页面
    @GetMapping("/types/{id}/update")
    public String updatePage(@PathVariable Long id, Model model){
        //查询分类信息传递到前端进行展示
        model.addAttribute("type",typeService.getType(id));
        return "admin/types-input";
    }

    //增加分类
    @PostMapping("/types/input")
    public String inputType(@Valid Type type, BindingResult result, RedirectAttributes attributes){
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1 != null) {//数据库中已存在该分类
            result.rejectValue("name","nameError","不能添加重复的分类");
        }
        if (result.hasErrors()) {//如果校验的结果有误
            return "admin/types-input";
        }
        Type t = typeService.saveType(type);
        if (t == null ) {
            attributes.addFlashAttribute("message", "添加分类失败");
        } else {
            attributes.addFlashAttribute("message", "添加分类成功");
        }
        return "redirect:/admin/types";
    }

    //更新分类
    @PostMapping("/types/update/{id}")
    public String updateType(@Valid Type type, BindingResult result,@PathVariable Long id, RedirectAttributes attributes){
        Type type1 = typeService.getTypeByName(type.getName());
        if (type1 != null) {//数据库中已存在该分类
            result.rejectValue("name","nameError","不能添加重复的分类");
        }
        if (result.hasErrors()) {//如果校验的结果有误
            return "admin/types-input";
        }
        Type t = typeService.updateType(id, type);
        if (t == null ) {
            attributes.addFlashAttribute("message", "更新分类失败");
        } else {
            attributes.addFlashAttribute("message", "更新分类成功");
        }
        return "redirect:/admin/types";
    }

    //删除分类
    @GetMapping("/types/delete/{id}")
    public String delete(@PathVariable Long id,RedirectAttributes attributes) {
        try{
            typeService.deleteType(id);
        }catch(Exception e){
            attributes.addFlashAttribute("message", "删除分类失败");
            return "redirect:/admin/types";
        }
        attributes.addFlashAttribute("message", "删除分类成功");
        return "redirect:/admin/types";
    }


}
